package com.xueqiu.movieplaymanagersystem.controller.admin;

import com.xueqiu.movieplaymanagersystem.service.PublicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import com.xueqiu.movieplaymanagersystem.controller.admin.publics.PublicController;
import com.xueqiu.movieplaymanagersystem.model.JsonResult;
import com.xueqiu.movieplaymanagersystem.entity.Tag;

/**
 * @author xueqiu
 */
@Controller
@RequestMapping("/admin")
public class TagController {
    private PublicService<Tag> publicService;

    @Autowired
    public void setPublicService(
            @Qualifier("tagService")
            PublicService<Tag> publicService
    ) {
        this.publicService = publicService;
    }

    /**
     * 新增标签
     * @param tag 标签名称
     * @return 新增成功与否
     */
    @GetMapping("/addOneTag/{tag}")
    @ResponseBody
    public JsonResult<Tag> save(
            @PathVariable(name = "tag")
            String tag
    ) {
        JsonResult<Tag> jsonResult = PublicController.isTrueString(tag);
        if (jsonResult != null) {
            return jsonResult;
        }
        jsonResult = new JsonResult<>();
        Tag originData = new Tag();
        originData.setTagName(tag);
        return PublicController.save(publicService, originData, jsonResult);
    }

    /**
     * 编辑标签
     * @param id 标签id
     * @param tagName 标签名称
     * @return 更新成功与否
     */
    @GetMapping("/editOneTag/{id}/{tagName}")
    @ResponseBody
    public JsonResult<String> edit(
            @PathVariable(name = "id") 
            String id,
            @PathVariable(name = "tagName") 
            String tagName
    ) {
        return PublicController.edit(id, tagName, publicService);
    }

    /**
     * 删除单个标签；根据标签id
     * @param id 标签id
     * @return 删除成功与否
     */
    @GetMapping("/deleteTag/{id}")
    @ResponseBody
    public JsonResult<String> deleteById(
            @PathVariable(name = "id") 
            String id
    ) {
        return PublicController.deleteById(id, publicService);
    }

    /**
     * 批量删除标签；根据标签名称
     * @param tags 标签名称
     * @return 删除成功与否
     */
    @PostMapping("/batchDeleteTag")
    @ResponseBody
    public JsonResult<String> deleteByIdBatch(
            @RequestParam(name = "tags[]") 
            String[] tags
    ) {
        return PublicController.deleteByIdBatch(tags, publicService);
    }

    /**
     * 单页查询标签、分页查询标签
     * @param tag 关键词
     * @param page 当前页
     * @param limit 每页条数
     * @return 单页查询数据或者多页查询中的某一页数据；可能为empty
     */
    @RequestMapping(value = "/tags", method = RequestMethod.GET)
    @ResponseBody
    public JsonResult<Tag> findAll(
            @RequestParam(required = false, name = "tag")
            String tag,
            @RequestParam(name = "page")
            int page,
            @RequestParam(name = "limit")
            int limit
    ) {
        return PublicController.findAll(tag, page, limit, publicService);
    }
}
